package com.heima.wemedia.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.wemedia.dtos.ChannelDto;
import com.heima.model.wemedia.pojos.WmChannel;
import com.heima.model.wemedia.pojos.WmNews;
import com.heima.wemedia.mapper.WmChannelMapper;
import com.heima.wemedia.mapper.WmNewsMapper;
import com.heima.wemedia.service.WmChannelService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

@Service
@Transactional
@Slf4j
public class WmChannelServiceImpl extends ServiceImpl<WmChannelMapper, WmChannel> implements WmChannelService {

    @Autowired
    private WmChannelMapper wmChannelMapper;

    @Autowired
    private WmNewsMapper wmNewsMapper;

    @Override
    public ResponseResult findAll() {
        Page<WmChannel> page = new Page<>(1,100);
        Page<WmChannel> wmChannelPage = wmChannelMapper.selectPage(page, null);

        return ResponseResult.okResult(wmChannelPage.getRecords());

    }

    @Override
    public ResponseResult saveChannel(WmChannel wmChannel) {
        if(wmChannel == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE,"频道名不能为空");
        }
        String name = wmChannel.getName();
        //去重
        LambdaQueryWrapper<WmChannel> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(StringUtils.isNotEmpty(name),WmChannel::getName,name);
        Integer count = wmChannelMapper.selectCount(wrapper);
        if(count > 0){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE,"频道已存在");
        }
        wmChannel.setCreatedTime(new Date());
        this.save(wmChannel);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    @Override
    public ResponseResult findList(ChannelDto dto) {

        if(dto == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        Integer page = dto.getPage();
        Integer size = dto.getSize();

        page = page == null ? 1:page;
        size = size == null ? 10:size;

        Page<WmChannel> channelPage = new Page<>(page,size);
        String name = dto.getName();
        LambdaQueryWrapper<WmChannel> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(StringUtils.isNotEmpty(name),WmChannel::getName,name);
        wrapper.orderByDesc(WmChannel::getCreatedTime);

        Page<WmChannel> wmChannelPage = wmChannelMapper.selectPage(channelPage, wrapper);
        //3.结果返回
        Long total = wmChannelPage.getTotal();
        PageResponseResult responseResult = new PageResponseResult(page, size, total.intValue());
        responseResult.setData(wmChannelPage.getRecords());
        return responseResult;
    }

    @Override
    public ResponseResult updateChannel(WmChannel wmChannel) {
        if(wmChannel == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }

        LambdaQueryWrapper<WmNews> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(WmNews::getChannelId,wmChannel.getId());
        wrapper.eq(WmNews::getStatus,9);
        Integer count = wmNewsMapper.selectCount(wrapper);
        if(count > 0){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE,"频道被引用");
        }
        this.updateById(wmChannel);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    @Override
    public ResponseResult delete(Integer id) {

        if(id == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        WmChannel wmChannel = wmChannelMapper.selectById(id);
        if(wmChannel == null){
            return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
        }
        if(wmChannel.getStatus()){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE,"频道为启用状态,不能删除");
        }

        LambdaQueryWrapper<WmNews> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(WmNews::getStatus,9);
        wrapper.eq(WmNews::getArticleId,wmChannel.getId());
        Integer count = wmNewsMapper.selectCount(wrapper);
        if(count > 0){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE,"频道被引用,不能删除");
        }
        boolean removeById = this.removeById(id);
        return removeById?ResponseResult.okResult(AppHttpCodeEnum.SUCCESS):ResponseResult.errorResult(AppHttpCodeEnum.SERVER_ERROR);
    }
}